package com.tudouji.framework.task;

import com.tudouji.common.utils.DateUtils;
import com.tudouji.project.bill.domain.Sellform;
import com.tudouji.project.bill.domain.SellformOverdue;
import com.tudouji.project.bill.mapper.SellformMapper;
import com.tudouji.project.bill.mapper.SellformOverdueMapper;
import com.tudouji.project.customer.domain.Customer;
import com.tudouji.project.customer.mapper.CustomerMapper;
import com.tudouji.project.pay.domain.SfPrebalanceRecord;
import com.tudouji.project.pay.service.ISfPrebalanceRecordService;
import com.tudouji.project.system.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 逾期处理
 * jsd
 */
@Slf4j
@Component("overdueTask")
public class OverdueTask {

    @Autowired
    private SellformMapper sellformMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private SellformOverdueMapper sellformOverdueMapper;
    @Autowired
    private ISysConfigService sysConfigService;

    public void overdue() {
        log.debug("___________________销售订单：逾期处理开始__________________");
        Date nowDay = DateUtils.getNowDay();
        sellformOverdueMapper.deleteSellformOverdueByCreateDate(nowDay);

        String overdueStartDate = this.sysConfigService.selectConfigByKey("overdue_start_date");
        List<Sellform> sellforms = sellformMapper.selectOverDueSellform(overdueStartDate);
        if (sellforms.size() > 0) {
            for (int i = 0; i < sellforms.size(); i++) {
                Sellform sellform = sellforms.get(i);
                Integer settlebilling = sellform.getSettlebilling();
                long actualPaymentDays = DateUtils.getDateDiff(nowDay, DateUtils.format(sellform.getLoadDate()));

                Sellform sfi = new Sellform();
                sfi.setId(sellform.getId());
                sfi.setActualPaymentDays((int)actualPaymentDays);
                sellformMapper.updateSellform(sfi);

                if (actualPaymentDays > settlebilling){
                    if (sellform.getOverdueFlag() == null || sellform.getOverdueFlag().intValue() == 0){
                        Sellform sf = new Sellform();
                        sf.setId(sellform.getId());
                        sf.setOverdueFlag(1);
                        sellformMapper.updateSellform(sf);
                    }

                    Customer customer = customerMapper.selectCustomerById(sellform.getCustomerguid());

                    SellformOverdue sod = new SellformOverdue();
                    sod.setSellformId(sellform.getId());
                    sod.setSellformCode(sellform.getSellcode());
                    sod.setCustomerId(customer.getId());
                    sod.setCustomerName(customer.getName());
                    sod.setDeptId(sellform.getDeptId());
                    sod.setDeptName(sellform.getDepname());
                    sod.setSubmitterid(sellform.getCreateBy());
                    sod.setSubmittername(sellform.getApplyname());
                    sod.setSettlementAmount(sellform.getFinalamount());
                    sod.setLoadDate(sellform.getLoadDate());
                    sod.setExpectPayDays(sellform.getSettlebilling());
                    sod.setOverdueDays((int)actualPaymentDays-settlebilling);
                    sod.setOverdueDate(DateUtils.addDay(sellform.getLoadDate(), settlebilling +1));
                    sod.setCreateTime(nowDay);
                    sellformOverdueMapper.insertSellformOverdue(sod);
                }
            }
        }

        log.info("___________________销售订单：逾期处理结束__________________");
    }

}
